(function ($) {
    $(window).load(function () {

        //侧边栏（待优化：点击document时隐藏）
        var
            oMain=$(".main"),
            oDiv=$(".sidebar .act:not(.ma)"),
            oTit=$(".sidebar .titbox"),
            oDit=$(".sidebar .act:lt(6)"),
            oHidebar=$(".hidbar"),
            oPay=$(".sidebar .pay"),
            oSideBar=$(".sidebar"),
            oHideMoney=$(".hide-money"),
            oHideClose=$(".hide-money .hm-close");

        oDiv.each(function(i){
            $(this).mouseenter(function () {
                oTit.eq(i).show().animate({
                    opacity: 1,
                    left: "-90px",
                })
            }).mouseleave(function(){
                oTit.eq(i).hide().animate({
                    opacity: 0,
                    left: "-100px",

                })
            })
        })

        oDit.click(function(){
            var
                oDitIndex=$(this).index(),
                oDitTop=oMain.position().top-180+oDit.eq(oDitIndex).position().top
                oDitTimeout=null;
            //console.log($(window).height()/2,oDitTop);
            clearTimeout(oDitTimeout);

            oHidebar.show().stop(true).animate({"top":oDitTop});

            oDitTimeout=setTimeout(function(){
                oHidebar.hide();
            },3000)

        })

        oPay.click(function(){
            if(parseInt(oSideBar.css('right'))==0){
                oSideBar.animate({right:235})
                oHideMoney.show().animate({right:0})
            }else if(parseInt(oSideBar.css('right'))==235){
                oSideBar.animate({right:0})
                oHideMoney.stop(true).animate({right:-235},function(){
                    oHideMoney.hide();
                });
            }
        })

        oHideClose.click(function(){
            oSideBar.stop(true).animate({right:0})
            oHideMoney.animate({right:-235},function(){
                oHideMoney.hide();
            });
        })






        // 搜索栏
        var 
            oTet=$(".tit"),
            oTetList=$('.tit-list'),
            iSerIndex= -1,
            a=1;
        oTet.focus(function(){

            //oTetList.css("display","block");
        }).keyup(function(e){
            var 
                evt= e || event,
                aList=$('.tit-list>li');
                //a=$(this).val();
            if(evt.keyCode===38 || evt.keyCode===40){
                if(evt.keyCode===38 && iSerIndex>-0){
                    iSerIndex--;
                    oTet.val(aList.removeClass("liactive").eq(iSerIndex).addClass("liactive").data("name"));
                    
                }else if(evt.keyCode===40 && iSerIndex<aList.length-1){
                    iSerIndex++;
                    oTet.val(aList.removeClass("liactive").eq(iSerIndex).addClass("liactive").data("name"));
                }
            }
            //console.log(aList.eq(0).data("name"))

        }).blur(function(){
            oTetList.css("display","none");
            oTet.val('');
        }).bind("input propertychange",function(){
            var 
                orValue=$(this).val();
            if(orValue){
                $.ajax({
                    url: "https://suggest.taobao.com/sug",

                    data: {q : orValue},

                    jsonp: 'callback',

                    jsonpcallback: 'jsonp540',

                    dataType: "jsonp",
                    success:function(data){
                        var sHtml='';
                        //console.log(data.result[0][1])
                        //sHtml+='<li data-name="'+data.q+'"><a href="#">'+data.q+'<span>约'+data
                        data.result.forEach(function(i){
                            sHtml+='<li data-name="'+i[0]+'"><a href="#">'+i[0]+'<span>约有'+i[1]+'个结果</span></a></li>'
                        })
                        if(sHtml){
                            oTetList.css("display","block").html(sHtml);
                        }else{
                            oTetList.css("display","none");
                        }
                        iSerIndex=-1;
                        
                    }

                })
            }else{
                oTetList.css("display","none")
            }

        })



        //滚动条事件
        var 
            oLink=$(".floor-box a:lt(5)"),
            oLinklast=$(".floor-box a:last"),
            oLift=$(".lift-box"),
            oFloor=$(".nav-floor-box div:gt(0)"),
            hidTop=$(".hid-header-wrap"),
            hidTet=$(".hid-tet"),
            hidTetlist=$(".hid-tetlist"),
            toTop=$(".totop");
        $(window).scroll(function(){
            var
                scrollT=$(window).scrollTop();
            //console.log(scrollT)

            if(scrollT<1000){
                oLink.removeClass();

                oLift.hide(500);

                toTop.hide().slideUp();
                hidTop.hide().slideUp();

            }else if(scrollT>=1000){
                oLift.show(500);
                toTop.show().slideDown();
                hidTop.show().slideDown();

                if(scrollT>1400 && scrollT<2300){
                    oLink.removeClass().eq(0).addClass("flactive1");//亲子1

                }else if(scrollT>2300 && scrollT<3100){
                    oLink.removeClass().eq(1).addClass("flactive2") ;//户外出行2

                }else if(scrollT>3100 && scrollT<3900){

                    oLink.removeClass().eq(2).addClass("flactive3");
                                                        // 打造爱巢
                }else if(scrollT>3900 && scrollT<4740){

                    oLink.removeClass().eq(3).addClass("flactive4");
                                                        // 居家生活
                }else if(scrollT>4740 && scrollT<5560){
                    oLink.removeClass().eq(4).addClass("flactive5");
                }
            }


        })
        oLinklast.click(function(){
            $("body").animate({
                "scrollTop" : 0,
            });
        })

        toTop.click(function(){
            $("body").animate({
                "scrollTop" : 0,
            });
        })

        oLink.click(function(){
            $("body").animate({
                scrollTop: 1840+$(this).index()*840,
            })
        })


        hidTet.focus(function(){
            hidTetlist.show();
        }).blur(function(){
            hidTetlist.hide();
            hidTet.val('');
        })



        // 放大镜
        var
            glass=$(".glass-pic"),
            oFot=$(".fot"),
            glassImg=$(".glass-pic img"),
            oFilter=$(".fil"),
            oSmallImg=$(".small-pic li"),
            oBigImg=$(".zoom");
        oSmallImg.mouseenter(function(){
            var ImgIndex=$(this).index();
            oSmallImg.removeClass("img-active").eq(ImgIndex).addClass("img-active");
            glassImg.attr({src:"img/jd"+(ImgIndex+1)+".jpg"});
        })

        glass.mouseenter(function(){
            oFilter.show();
        }).mousemove(function(e){
            var 
                evt = e || window.event;
                disx=evt.pageX-oFot.offset().left-oFilter.outerWidth()/2,
                disy=evt.pageY-oFot.offset().top-oFilter.outerHeight()/2;
                console.log(disx,disy);
            oFilter.css({
                left: disx,
                top: disy,
            })
            console.log(disx,disy)
            
            if(disx<0){
                disx=0;
            }else if(disx>glass.width()-oFot.outerWidth()){
                disx=glass.width()-oFot.outerWidth();
            }else{
                disx=evt.pageX-oFot.offset().left-oFilter.outerWidth()/2
            }

            if(disy<0){
                disy=0;
            }else if(disy>glass.height()-oFot.outerHeight()){
                disy=glass.height()-oFot.outerHeight();
            }else{
                disy=evt.pageY-oFot.offset().top-oFilter.outerHeight()/2
            }           

           // console.log(disx,disy)
            
            
        }).mouseleave(function(){
            //console.log(10);
            oFilter.hide();
        })




        //评论翻页


        var
            oComdiv=$(".comment-main"),
            oPagepre=$(".bottom #page-pre"),
            oPageNext=$(".bottom #page-next"),
            oPage=$(".bottom a"),
            oComDay=$("#commen-list .comment-day"),
            oComtime=$("#commen-list .comment-time"),
            oPtab=$("#commen-list .p-tab"),
            oPcom=$("#commen-list .p-comment"),
            oUserImg=$("#commen-list .user-ico"),
            oUserName=$("#commen-list .user-name"),
            oLevel=$("#commen-list .vip-level"),
            oUsermethod=$("#commen-list .user-access"),
            oCm2=$("#commen-list .cm2"),
            oComoper=$("#commen-list .comment-oper"),
            PageIndex=0;

        //console.log(oComDay);
        oPage.click(function(){
            PageIndex=$(this).index(".bottom a");
            oPage.removeClass().eq(PageIndex).addClass("aactive");
            //console.log(oPage.eq(PageIndex).attr("rel"),oPagepre)

            if(oPage.eq(PageIndex).attr("rel")>1){
                oPagepre.show();
            }else{
                oPagepre.hide();
            }
            oPagepre.attr("rel",PageIndex);
            oPageNext.attr("rel",(PageIndex+2));
            datas(PageIndex);
        })

        oPagepre.click(function(){
            PageIndex--;
            if(PageIndex<=0){
                PageIndex=0;
                oPagepre.hide();
                oPagepre.attr("rel",PageIndex);
            }
            //console.log(PageIndex)
            oPage.removeClass().eq(PageIndex).addClass("aactive");
            oPagepre.attr("rel",PageIndex);
            oPageNext.attr("rel",(PageIndex+2));
            datas(PageIndex);
        })

        oPageNext.click(function(){
            oPagepre.show();
            PageIndex++;
            oPage.removeClass().eq(PageIndex).addClass("aactive");
            oPagepre.attr("rel",PageIndex);
            oPageNext.attr("rel",(PageIndex+2));
            datas(PageIndex);
        })



        function datas(Index){
            $.ajax({
                url: "http://sclub.jd.com/productpage/p-11241687-s-0-t-3-p-"+Index+".html",
                //json: {},
                jsoncallback: "fetchJSON_comment98vv235",
                dataType: "jsonp",
                success:function(data){
                    console.log(data.comments[0].userLevelName)

                            var i=data.comments;
                            //console.log(i);
                            for(var k= 0,len= i.length; k<len;k++){
                                var sHtml='';

                                oComDay.eq(k).html("下单" + i[k].days + "天后评论");

                                oComtime.eq(k).html(i[k].referenceTime);
                                if(i.commentTags){
                                    for(var j= 0,len=i[k].commentTags.length; j<len; j++){
                                        sHtml+='<em>'+i[k].commentTags[j].name+'</em>'
                                    }
                                }
                                oPtab.eq(k).html(sHtml);

                                oPcom.eq(k).html(i[k].content);

                                oUserImg.eq(k).attr("src", "//"+i[k].userImage);

                                oUserName.eq(k).html(i[k].nickname);

                                oLevel.eq(k).html(i[k].userLevelName);

                                oUsermethod.eq(k).html(i[k].userClientShow);
                            }

                }
            })
        }
        /**/





    })
})(jQuery)